package org.sun.sample.pojo;

import org.sun.sample.util.IRequest;
import org.sun.sample.util.IResponse;

import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

/**
 * 反射
 */
public class ReflectSample implements IResponse, IRequest{

    public String publicAttr;

    protected String protectedAttr;

    private String privateAttr;

    public ReflectSample() {

        //获取到class对象
        Class c = ReflectSample.class;

        //获取class对象的父类
        print("获取class对象的父类.....................");
        print(c.getSuperclass());

        print("获取class的开放的属性.....................");
        //获取class的开放的属性
        Field[] fields = c.getFields();
        for (int i=fields.length-1; i>=0; i--) {
            print(fields[i].getName());
        }

        print("获取class的所有属性.....................");
        //获取class的所有属性
        fields = c.getDeclaredFields();
        for (int i=fields.length-1; i>=0; i--) {
            print(fields[i].getName());
        }

        print("获取class的开放方法.....................");
        Method[] methods = c.getMethods();
        for (int i=methods.length-1; i>=0; i--) {
            print(methods[i].getName());
        }

        print("获取class的所有方法.....................");
        methods = c.getDeclaredMethods();
        for (int i=methods.length-1; i>=0; i--) {
            print(methods[i].getName());
        }

        print("获取class的开放构造方法.....................");
        Member[] constructors = c.getConstructors();
        for (int i=constructors.length-1; i>=0; i--) {
            print(constructors[i].getName());
        }

        print("获取class的所有构造方法.....................");
        constructors = c.getDeclaredConstructors();
        for (int i=constructors.length-1; i>=0; i--) {
            print(constructors[i].getName());
        }

        print("获取class的所有接口.....................");
        Class[] interfaces = c.getInterfaces();
        for (int i=interfaces.length-1; i>=0; i--) {
            print(interfaces[i].getName());
        }
    }

    private ReflectSample(int index) {

    }


    protected ReflectSample(String index) {

    }

    public void publicMethod() {

    }

    private void privateMethod() {

    }

    protected void protectedMethod() {

    }

    public void print(Object message) {
        System.out.println(message.toString());
    }

}
